// pages/goods-specs/index.js
import {
    ReDistributionMall
} from "../../api/shipping.js"
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        show: {
            type: Boolean,
            value: false
        },
        btnText: {
            type: String,
            value: ''
        },
        dataObj: {
            type: Object,
            value: {}
        },
        // 规格商品列
        option: {
            type: Array,
            value: []
        },
        disabled: {
            type: Boolean,
            value: false
        },
    },

    /**
     * 组件的初始数据
     */
    data: {
        isShow: false,
        active: 0,
        activeChild: 0,
        goodsList: [{
            goodsImg: "/images/element_christmas_xpzq@2x.png",
            goodsName: "商品名称商品名称商品名称商品名称商品名称商品名称商品名称商品名称商品名称",
            goodsPrice: "310.00",
            goodsSpecs: "规格规格规格个规格规格规格规格规格规格个规格规格规格",
            goodsSpecsCount: 2
        }, ],
        specsList: ['2.5kg', '3.5kg'],
        active: 0, //当前选中规格
        specsName: "",
        count: 1,
        thumb: '',
        property_price: '',
        propertydata: {}
    },

    observers: {
        'dataObj': function (newVal, oldVal) {
            console.log('接收到的对象：', newVal);
            this.setData({
                thumb: newVal.property[0].child[0].img || '',
                property_price: newVal.property[0].child[0].price
            });
        },
        'show': function (newVal, oldVal) {
            if (newVal && this.data.dataObj.tips == 0) { // 普通
                this.printImageInfo();
            } else if (newVal && this.data.dataObj.tips == 1) { //一件代发
                if (this.data.dataObj.property[this.data.active].child[this.data.activeChild] != undefined) {
                    this.data.propertydata = {
                        productid: this.data.dataObj.property[this.data.active].child[this.data.activeChild].id
                    }
                    let obj = {
                        id: this.data.dataObj.id,
                        propertyid: this.data.propertydata
                    }
                    this.getOneGoodsLists(obj)
                } else {
                    this.data.propertydata = {
                        productid: this.data.dataObj.propertyid
                    }
                    let obj = {
                        id: this.data.dataObj.id,
                        propertyid: this.data.propertydata
                    }
                    this.getOneGoodsLists(obj)
                }
            } else if (newVal && this.data.dataObj.tips == 2) { //京东
                this.data.propertydata = {
                    id: this.data.dataObj.property[this.data.active].child[this.data.activeChild].id
                }
            }
            this.setData({
                isShow: newVal
            })

        }
    },
    /**
     * 组件的方法列表
     */
    methods: {
        printImageInfo() {
            this.setData({
                thumb: this.data.dataObj.property[this.data.active].child[this.data.activeChild].img || '',
                property_price: this.data.dataObj.property[this.data.active].child[this.data.activeChild].price
            });
        },

        //获取一件代发购物车商品数量
        getOneGoodsLists(item) {
            ReDistributionMall({
                act: 'oneGoodsInfo',
                id: item.id,
                propertyid: item.propertyid.productid

            }).then(res => {
                console.log('oneGoodsInfo', res)
                if (res.cart_numbers == '0') {
                    res.cart_numbers = 1
                }
                wx.nextTick(() => {
                    this.setData({
                        count: res.cart_numbers || 1
                    })
                })
            });
        },


        /**
         * 选择规格回调函数方法
         * @param {*} e 
         */
        handleSelSpecs(e) {
            console.log('切换', e)
            console.log(this.data.dataObj, this.data.activeChild)

            const index = e.currentTarget.dataset.index;
            this.data.dataObj.propertyid = this.data.dataObj.property[index].child[this.data.activeChild].id
            this.data.dataObj.propertyname = e.currentTarget.dataset.value.property_name

            let data = this.data.dataObj.property[index]
            this.data.dataObj.inventory = data.child[this.data.activeChild].inventory
            if (data.child[this.data.activeChild].inventory == 0) {

                this.setData({

                    disabled: true,
                    btnText: "已售罄"
                })
            } else {

                this.setData({

                    disabled: false,
                    btnText: "加入购物车"
                })
            }
            this.setData({
                active: index,
                activeChild: 0, // 重置第二个规格栏的选中状态
                thumb: this.data.dataObj.property[index].child[this.data.activeChild].img,
                property_price: this.data.dataObj.property[index].child[this.data.activeChild].spec,
                count: this.data.dataObj.property[index].child[this.data.activeChild].number,
            });
        },
        // handleSelSpecs(e) {
        //     const index = e.currentTarget.dataset.index;
        //     this.setData({
        //         active: index,
        //         activeChild: 0, // 重置第二个规格栏的选中状态
        //         thumb: this.data.dataObj.property[index].child[this.data.activeChild].img,
        //         property_price:this.data.dataObj.property[index].child[this.data.activeChild].price
        //     });

        // },
        handleSelSpecsChild(e) {
            const index = e.currentTarget.dataset.index;
            this.setData({
                activeChild: index,
                property_price: this.data.dataObj.property[this.data.active].child[index].price
            });
            console.log('e', e)
            this.data.propertydata = {
                ...e.currentTarget.dataset.value
            }
        },
        /**
         * 商品数量变化回调函数方法
         * @param {*} e 
         */
        handleNumChange(e) {
            this.setData({
                count: e.detail
            })
        },
        /**
         * 确认按钮回调函数方法
         */
        onConfirm() {
            let num
            // tips = 0商品列表 tips = 1 一件代发 
            console.log('this.data.dataObj.tips', this.data.dataObj)
            if (this.data.dataObj.tips == 1) {
                num = 1
            } else {
                num = 0
            }
            this.triggerEvent('confirm', {
                index: this.data.active,
                name: this.data.specsList[this.data.active],
                data: this.data.propertydata,
                count: this.data.count || 1,
                tips: num,
                commodityId: this.data.dataObj.id
            })
        },
        /**
         * 关闭
         */
        close() {
            this.triggerEvent('close')
        }
    }
})